﻿$(document).ready(function () {

    $(function () {
        $.ajaxSettings.cache = false;
        $.ajaxSettings.jsonp = undefined;
        $.ajaxSettings.jsonpCallback = undefined;
    })

    $('#main').find('[name=save_page]').click(function (event) {
        event.preventDefault();      
            $.ajax({
                url: window.savePage,
                type: "POST",
                dataType: "json",
                cache: self.cacheEnable,
                data: {
                    "Page.Id": $('#main').find('[id=Page_Id]').val(),
                    "Page.CreatedAt": $('#main').find('[id=Page_CreatedAt]').val(),
                    "Page.Alias": $('#main').find('[id=Page_Alias]').val(),
                    "Page.ModifiedAt": $('#main').find('[id=Page_ModifiedAt]').val(),
                    "Page.AuthorId": $('#main').find('[id=Page_AuthorId]').val(),
                    "Page.PageTitle": $('#main').find('[id=Page_PageTitle]').val(),

                    "Page.PageContent": CKEDITOR.instances['Page_PageContent'].getData(),
                    "Tags": $('#main').find('[id=Tags]').val(),
                    "Page.SeoTitle": $('#main').find('[id=Page_SeoTitle]').val(),
                    "Page.MetaKeywords": $('#main').find('[id=Page_MetaKeywords]').val(),
                    "Page.MetaDescription": $('#main').find('[id=Page_MetaDescription]').val(),
                    "Page.StatusId": $('#main').find('[id=StatusId]').val(),
                    "Page.CategoryId": $('#main').find('[id=CategoryId]').val()
                },

                success: function (response) {
                    if (response.success) {
                       $(document).load().scrollTop(0);
                       ShowSuccess();
                    }
                },
                error: function (XMLHttpRequest, etype, exo) {
                    alert(etype, exo);
                    return;
                }
            });
        return false;
    });

    $('#main').find('[name=save_post]').click(function (event) {
        event.preventDefault();
        $.ajax({
            url: window.savePost,
            type: "POST",
            dataType: "json",
            cache: self.cacheEnable,
            data: {
                "Page.Id": $('#main').find('[id=Page_Id]').val(),
                "Page.Created": $('#main').find('[id=Page_Created]').val(),
                "Page.HeaderUrl": $('#main').find('[id=Page_HeaderUrl]').val(),
                "Page.Modified": $('#main').find('[id=Page_Modified]').val(),
                "Page.Author": $('#main').find('[id=Page_Author]').val(),
                "Page.Header": $('#main').find('[id=Page_Header]').val(),
                "Page.ShortContent": CKEDITOR.instances['Page_ShortContent'].getData(),
                "Page.Content": CKEDITOR.instances['Page_Content'].getData(),
                "Tags": $('#main').find('[id=Tags]').val(),
                "Page.SeoTitle": $('#main').find('[id=Page_SeoTitle]').val(),
                "Page.MetaKeywords": $('#main').find('[id=Page_MetaKeywords]').val(),
                "Page.MetaDescription": $('#main').find('[id=Page_MetaDescription]').val(),
                "Page.Visible": $('#main').find('[id=Page_Visible]').val(),
                "Page.CategoryId": $('#main').find('[id=CategoryId]').val()
            },

            success: function (response) {
                if (response.success) {
                    $(document).load().scrollTop(0);
                    ShowSuccess();
                }
            },
            error: function (XMLHttpRequest, etype, exo) {
                alert(etype, exo);
                return;
            }
        });
        return false;
    });

    $('#main').find('[name=save_menu]').click(function (event) {
        event.preventDefault();      
 //       if ($("#editMenuItemForm").valid()) {
        if ($('#MenuItem_IsExternalUrl').is(':checked')) {
            if ($('#MenuItem_NavigateUrl').val()=='') {
                alert("Enter navigate url");
                    return false;
            }
        }
        else {
            if ($('#MenuItem_EntryId').val()==0) {
                alert("Selected page");
                return false;
            }
        }
        
        $.ajax({
            url: window.saveMenuItem,
            type: "POST",
            dataType: "json",
            cache: self.cacheEnable,
            data: {
                "MenuItem.Id": $('#main').find('[id=MenuItem_Id]').val(),
                "MenuItem.MenuId": $('#main').find('[id=MenuItem_MenuId]').val(),
                "MenuItem.CreatedAt": $('#main').find('[id=MenuItem_CreatedAt]').val(),
                "MenuItem.ParentId": $('#main').find('[id=MenuItem_ParentId]').val(),
                "MenuItem.Title": $('#main').find('[id=MenuItem_Title]').val(),
                "MenuItem.IsExternalUrl": $('#main').find('[id=MenuItem_IsExternalUrl]').is(':checked'),
                "MenuItem.NavigateUrl": $('#main').find('[id=MenuItem_NavigateUrl]').val(),
                "MenuItem.EntryId": $('#main').find('[id=MenuItem_EntryId]').val(),
                "MenuItem.Visible": $('#main').find('[id=MenuItem_Visible]').is(':checked'),
                "MenuItem.SortOrder": $('#main').find('[id=MenuItem_SortOrder]').val(),
            },

            success: function (response) {
                if (response.success) {
                    document.getElementById("MenuItem_Id").value = response.id;
                    $(document).load().scrollTop(0);
                    ShowSuccess();
                }
            },

            error: function (XMLHttpRequest, etype, exo) {
                alert(etype, exo);
                return;
            }
        });
    //    }
        return false;
    });

    $('#main').find('[name=save_setting]').click(function (event) {
        event.preventDefault();      

        if ($("#editSiteSettingForm").valid()) {
            $.ajax({
                url: window.saveSetting,
                type: "POST",
                dataType: "json",
                cache: self.cacheEnable,
                data: {
                    "Id": $('#main').find('[id=Id]').val(),
                    "CreatedAt": $('#main').find('[id=CreatedAt]').val(),
                    "Key": $('#main').find('[id=Key]').val(),
                    "Value": $('#main').find('[id=Value]').val(),
                },

                success: function (response) {   
                    if (response.success) {
                        $('#main').find('[id=Id]').val(response.id);
                        $(document).load().scrollTop(0);
                        ShowSuccess();
                    }
                },
                error: function (XMLHttpRequest, etype, exo) {
                    alert(etype, exo);
                    return;
                }
            });
        }
        return false;
    });

    $('#main').find('[name=save_user]').click(function (event) {
        event.preventDefault();
    
      if ($("#editUserForm").valid()) {  

          var id = $('#main').find('[id=Id]').val();
          var password = $('#main').find('[id=Password]').val();
          var currentPassword = $('#main').find('[id=CurrentPassword]').val();

          if (id == 0 && password == '') { alert("Enter a password"); return false; }
          if (password != '') {
              if (password.length < 6) {
                  alert("Password must be at least 6 chars"); return false;
              }
              if (id != 0 && currentPassword == '') {
                  alert("Please enter current password"); return false;
              }
          }

        $.ajax({
            url: window.saveUser,
            type: "POST",
            dataType: "json",
            cache: self.cacheEnable,
            data: {
                "Id": $('#main').find('[id=Id]').val(),
                "Hash": $('#main').find('[id=Hash]').val(),
                "Login": $('#main').find('[id=Login]').val(),
                "DisplayName": $('#main').find('[id=DisplayName]').val(),
                "Email": $('#main').find('[id=Email]').val(),
                "Password": $('#main').find('[id=Password]').val(),
                "CurrentPassword": $('#main').find('[id=CurrentPassword]').val()
            },

            success: function (response) {
                if (response.success) {
                    $('#main').find('[id=Id]').val(response.id);
                    $('#main').find('[id=CurrentPasswordSet]').css("display", "block");
                    $('#main').find('[id=Password]').val('');
                    $(document).load().scrollTop(0);
                    ShowSuccess();
                }
            },
            error: function (XMLHttpRequest, etype, exo) {
                alert(etype, exo);
                return;
            }
        });
      }
        return false;
    });

    $('#main').find('[name=save_role]').click(function (event) {
        event.preventDefault();

        var id= $('#main').find('[id=RoleId]').val();
        var name = $('#main').find('[id=RoleName]').val();

        if ($("#editRoleForm").valid()) {
            
            $.ajax({
                url: window.saveRole,
                type: "POST",
                dataType: "json",
                cache: self.cacheEnable,
                data: {
                    "RoleId": id == '' ? 0 : id ,
                    "RoleName": name
                },

                success: function (response) {
                    if (response.success) {
                        $('#main').find('[id=RoleId]').val(response.id)
                        $(document).load().scrollTop(0);
                        ShowSuccess();
                    }
                },
                error: function (XMLHttpRequest, etype, exo) {
                    alert(etype, exo);
                    return;
                }
            });
        }
        return false;
    });

    function ShowSuccess(){
        $('#main').find('[id=successResult]').fadeIn(1000, function () {
            $('#main').find('[id=successResult]').css('display','block');
        }).delay(2000).fadeOut(1000, function () {
            $('#main').find('[id=successResult]').css('display','none');
        });
    };
});